package com.hxalex.simulationhandle.util;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import com.hxalex.simulationhandle.R;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by hx_Alex on 2018/6/5.
 */

public class SystemALertDialog extends AlertDialog {
    public SystemALertDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activation);
        intiWondow();
    }

    private void  intiWondow()
    {
        Window window = getWindow();
        setCanceledOnTouchOutside(true);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            //半透明头部状态栏，底部导航栏   布局在状态栏，导航栏下方
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        window.getDecorView().setFitsSystemWindows(true);
        window.setBackgroundDrawable(getContext().getResources().getDrawable(R.color.transparent));
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width= WindowManager.LayoutParams.MATCH_PARENT;
        lp.height= WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);
        window. setType(WindowManager.LayoutParams.TYPE_TOAST);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            window. setType (WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
            return;
        }
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N ){//6.0
            window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        }else {
            window.setType(WindowManager.LayoutParams.TYPE_TOAST);
        }
    }

    @Override
    public void show() {
        super.show();
        Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if(isShowing()){
                    dismiss();
                }
            }
        },1000);
    }
}
